<?php
include_once "../../qc_base.php";
include_once "../../qc_utils.php";
include_once "../../bootstrap.php";

include_once "../../classes/MakePage.php";


function makesuredir($dir){
	if(!file_exists(dirname($dir))){
		makesuredir(dirname($dir));
	}
	if(!file_exists($dir)){
		mkdir($dir);
	}
}

function write_pdoaj_proc_to_file($crudset_id){

	$qcs = Doctrine_Query::create()
	->from('Crudsets cs')->where('crudset_id = ?',$crudset_id);
	$setinfo = $qcs->fetchOne();

	// get the main table for the set
	$qtb = Doctrine_Query::create()
	->from('Tablesources tb')->where('tablesource_id = ?',$setinfo['main_table']);
	$tablesource = $qtb->fetchOne();
	$table_name = $tablesource['table_name'];	
	
	$q = Doctrine_Query::create()
	->from('Projects p')->where('project_id = ?',$setinfo['project_id']);
	$project = $q->execute()->getFirst();	
	
	$project_db = $project->project_db;
	$project_path = $project->project_path;
	$project_server = $project->project_server;
	$project_login = $project->project_login;
	$project_pass = $project->project_pass;
   	$project_name = $project->project_name;
	$class_subpath = $project->class_subpath;
	$template_dir = $project->template_dir;	
	$structure = $project->file_structure;

	$structure_path = QC_SYS_BASEDIR . '/structures/';
	
	include $structure_path . $structure .'/paths.php';
	

	$outpath = $paths['modules'];
	$outpath = str_replace('<setname>',$setinfo['cs_name'],$outpath);
	
	$outpath = QC_SYS_BASEDIR . '/' . QC_PROJECT_SUBPATH . $project_name . '/' . $outpath ;

	makesuredir($outpath);
	
	
	$targetfile = $outpath . $setinfo['cs_name'] . '_proc.php';
	$tplfile = 'pdoaj_proc_tpl.php';

//--------------------Create Replacement Text



$includefieldspec = "include '" . $setinfo['cs_name'] . "_fields.php';";
$setcsname = '
// SET THE NAME 
'."\$cs_name = '" . $setinfo['cs_name'] . "';";

$setcstable= '
// SET THE Table 
'."\$cs_table = '" . $table_name . "';";

$filtering = '
$' . $setinfo['cs_name'] . '_Filtering = new CF_FilterDataPdo($proc->filters); 
';

$ClassCrud = '
class ' . $setinfo['cs_name'] . 'Crud extends CF_Crud_Pdo {
';

$ClassClose = '
}
';


$ClassProc = '
class ' . $setinfo['cs_name'] . 'Processor extends CF_ProcessorCrud {


	function process_jsonlookup(){
		$col = $_REQUEST[\'col\'];
		$lookup = new ' . $setinfo['cs_name'] . 'LookupList;
		$list = $lookup->GetLookupList($col);
		echo json_encode($list);
		exit;
	
	}
				
	function process_list(){
		header(\'Location: ../mainpage.php\');
	}

// process request to set filter
	function process_filter(){
				$this->GetReqFilters();
				echo json_encode($this->filters);
				
	}

	function process_jsonp(){
		$this->GetReqFilters();
		
		$lister = new ' . $setinfo['cs_name'] . 'DataLister;
		$lister->set_filters($this->filters);

		$new_list = $lister->GetDataList();
		
		$listfiller = new ' . $setinfo['cs_name'] . 'Array($new_list,$this->fieldspec);
		$new_list = $listfiller->fill_spec_array();
		//$new_list = $this->fill_spec_array($new_list);

		echo $_GET["jsoncallback"] . "(" . json_encode($new_list) . ")";
		exit;
		

	}
';
if(!empty($setinfo['query'])){
$BaseQuery = '
		$qry_list = "' . $setinfo['query'] . ' ";
';
}else{
$BaseQuery = '
		$qry_list = "SELECT * FROM ' . $table_name . ' WHERE 1=1 ";
';
}


$ProcSetup = '
$proc = new ' . $setinfo['cs_name'] . 'Processor($' . $setinfo['cs_name'] . '_spec,$cs_name );
';

$CrudSetup = '
$sd = new ' . $setinfo['cs_name'] . 'Crud($proc);
';

$heading = '
	//$qry_list .= $Heading->make_query_sort();	
	//$qry_list = limit_query($qry_list, Paging->startat, Paging->numrows );
	//$qry_list = limit_query($qry_list, 2, 5 );';


$tableclass = 'class ' . $setinfo['cs_name'] . '_table extends ' . $setinfo['class_base_name'] .  '{';

$tablesetup = '$' . $setinfo['cs_name'] . '_table = new ' . $setinfo['cs_name'] . '_table($' . $setinfo['cs_name'] . '_spec,$none,$name);
$' . $setinfo['cs_name'] . '_table->set_valuesarray($list);';

$tableprint = 'print $' . $setinfo['cs_name'] . '_table->maketable();';


//---------------------------Write into file	
	
	

	$pagemaker = new MakePage();

//	$pagemaker->AddReplacement('includefieldspec',$includefieldspec);
	$pagemaker->AddReplacement('setcsname',$setcsname);	
	$pagemaker->AddReplacement('setcstable',$setcstable);	
	$pagemaker->AddReplacement('ClassCrud',$ClassCrud);
	$pagemaker->AddReplacement('ClassClose',$ClassClose);
	$pagemaker->AddReplacement('ClassProc',$ClassProc);
	$pagemaker->AddReplacement('ProcSetup',$ProcSetup);
	$pagemaker->AddReplacement('CrudSetup',$CrudSetup);
	$pagemaker->AddReplacement('BaseQuery',$BaseQuery);	
	
	$newstuff = $pagemaker->ReplaceInFile($tplfile,$targetfile);
	
	$pagemaker->PrintReplacementsTable();

	echo '<pre>' . $pagemaker->str_log . '</pre>';
	
	file_put_contents($targetfile,$newstuff);	

	return $newstuff;


}
?>
